import 'package:daysmatter/detailspage.dart';
import 'package:daysmatter/routes/EnterRouter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cupertino_datetime_picker/flutter_cupertino_datetime_picker.dart';
import 'bean/dateinfo.dart';
import 'searchappbar.dart';
import 'servicepath.dart';
import 'menu_item.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List? _getdata = [];
  DateTime dateTime = DateTime.now();
  Data? data;
  String? msg = '';
  int? code = 0;
  String? searchValue = '';
  //?token=qlVquQZPYSeaCi6u&month=5&day=20
  void initState() {
    super.initState();
    var jsondata = {
      "day": dateTime.day,
      "month": dateTime.month,
      "token": "qlVquQZPYSeaCi6u"
    };
    getInfo(jsondata);
  }

  void getInfo(var jsondata) async {
    DateInfo dateInfo = await getDateInfo(jsondata);
    setState(() {
      msg = dateInfo.msg;
      code = dateInfo.code;
      if (code == 200) {
        _getdata = dateInfo.data!;
        print(_getdata);
      } else {}
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          // 清除 title 左右的 padding，默认会有一定的距离
          titleSpacing: 0,
          elevation: 0,
          title: SearchAppBar(
            hintLabel: "请输入搜索内容",
            onSubmitted: (value) {
              setState(() {
                this.searchValue = value;

                var jsondata = {
                  "word": this.searchValue,
                  "page": "1",
                  "token": "qlVquQZPYSeaCi6u"
                };
                getInfo(jsondata);
              });
            },
            dateButttonCallback: (value) async {
              DatePicker.showDatePicker(context,
                  dateFormat: "yyyy年 MM月 dd日", //通过空格设置滚轮数量
                  pickerTheme: const DateTimePickerTheme(
                      //样式设置，可以设置确认按钮、取消按钮、标题等,
                      cancel: Text("取消"),
                      confirm: Text("确认",
                          style: TextStyle(fontSize: 14, color: Colors.black))),
                  onConfirm: (DateTime dateTime, List<int> selectedIndex) {
                setState(() {
                  // 确认按钮回调，获取选择结果 dateTime
                  var jsondata = {
                    "day": dateTime.day,
                    "month": dateTime.month,
                    "token": "qlVquQZPYSeaCi6u"
                  };
                  getInfo(jsondata);
                });
              });
            },
          ),
        ),
        body: Container(
          child: ListView.builder(
              itemCount: (_getdata == null) ? 0 : _getdata!.length,
              itemBuilder: (BuildContext context, int position) {
                /* data=_getdata[position];
              return  JobList(data);*/
                return getItem(position);
                //getItem(position);
              }),
        ));
  }

  Widget getItem(int index) {
    data = _getdata![index];
    return GestureDetector(
      child: Padding(
        padding:
            const EdgeInsets.only(top: 3.0, left: 5.0, right: 5.0, bottom: 3.0),
        child: MenuItem(
          icon: Icons.title,
          title: data!.title!,
        ),
      ),
      onTap: () {
        onItemclick(index);
      },
    );
  }

  void onItemclick(int index) {
    data = _getdata![index];
    print("获取到的头ID" + data!.id!);

    // Navigator.push(
    //     context,
    //     EnterExitRoute(
    //         exitPage: MyHomePage(),
    //         enterPage: Detailspage(
    //           arguments: data!.id!,
    //         )));

    Navigator.of(context)
        .pushNamed("detailspage", arguments: {"id": data!.id!});
  }
}
